
import java.io.*;
import java.util.*;
/**
 *
 * @author Prateek
 */
public class DataSmooth
{
    String fileName;
    int order;
    int selectInterval;
    boolean flagWaitQueueLength;
    boolean flagThr;
    boolean flagCoVar;

    DataSmooth(String fileName, int order, int selectInterval, 
            boolean flagWaitQueueLength, boolean flagThr, boolean flagCoVar)
    {
        this.fileName = fileName;
        this.order = order;
        this.selectInterval = selectInterval;
        this.flagWaitQueueLength = flagWaitQueueLength;
        this.flagThr = flagThr;
        this.flagCoVar = flagCoVar;
    }

    public void MoveAverage(double[] values, ArrayList<Double> al, int order,
            int numLine)
    {
        double firstValue = 0.0;
        for(int i = 0;i < order - 1;i++)
        {
            values[i] = al.get(i);
            firstValue += values[i];
        }
        firstValue += al.get(order - 1);
        values[order - 1] = firstValue / order;
        for(int i = order;i < numLine;i++)
        {
            values[i] = (values[i - 1] * order - al.get(i - order) +
                    al.get(i)) / order;
            if(values[i] < 0)
            {
                values[i] = 0;
            }
        }
    }

    public static void main(String[] args) throws IOException
    {
        DataSmooth ds = new DataSmooth("summary_1024_factor_1200_4500_18000.txt",
                60, 10, true, true, false);
        BufferedReader br = null;
        BufferedWriter bw = null;
        int numLine = 0;
        ArrayList<String> lineString = new ArrayList<String>();
        ArrayList<Double> alThroughput = new ArrayList<Double>();
        ArrayList<Double> alWaitQueue = new ArrayList<Double>();
        ArrayList<Double> alCoVar = new ArrayList<Double>();
        String line = null;
        try
        {
            br = new BufferedReader(new FileReader(ds.fileName));
            bw = new BufferedWriter(new FileWriter("newText.txt"));
        }
        catch(FileNotFoundException e)
        {
            e.printStackTrace();
            return;
        }
        while((line = br.readLine()) != null)
        {
            numLine++;
            lineString.add(line);
            String[] str = line.split("\t");
            alWaitQueue.add(Double.parseDouble(str[8]));
            alThroughput.add(Double.parseDouble(str[13]));
            alCoVar.add(Double.parseDouble(str[31]));
        }
        double[] moveAverageThroughput = new double[numLine];
        double[] moveAverageWaitQueue = new double[numLine];
        double[] moveAverageCoVar = new double[numLine];
        if(ds.order < 1 || ds.order > numLine)
        {
            System.out.println("Order must be at least 1 and could not "
                    + "exceed number of lines");
            return;
        }
        if(ds.flagWaitQueueLength)
        {
            ds.MoveAverage(moveAverageWaitQueue, alWaitQueue,
                    ds.order, numLine);
        }
        if(ds.flagThr)
        {
            ds.MoveAverage(moveAverageThroughput, alThroughput,
                    ds.order, numLine);
        }
        if(ds.flagCoVar)
        {
            ds.MoveAverage(moveAverageCoVar, alCoVar, ds.order, numLine);
        }
        int nextRowIncrem = ds.selectInterval + 1;
        int i,j;
        for(i = 0;i < ds.order - 1;i += nextRowIncrem)
        {
            bw.write(lineString.get(i) + "\r\n");
        }
        if(i < numLine)
        {
            for(j = i;j < numLine;j += nextRowIncrem)
            {
                String[] str = lineString.get(j).split("\t");
                if(ds.flagWaitQueueLength)
                {
                    str[8] = Double.toString(moveAverageWaitQueue[j]);
                }
                if(ds.flagThr)
                {
                    str[13] = Double.toString(moveAverageThroughput[j]);
                }
                if(ds.flagCoVar)
                {
                    str[31] = Double.toString(moveAverageCoVar[j]);
                }
                String newLine="";
                for(int k = 0;k < str.length - 1;k++)
                {
                    newLine += str[k] + "\t";
                }
                newLine += str[str.length - 1] + "\r\n";
                bw.write(newLine);
            }
        }
        br.close();
        bw.flush();
        bw.close();
    }
}
